﻿using System.Collections.Generic;
using System.Data;
using Loki.Data;
using Luna.Model.Common;

namespace Luna.Data.Common
{
    /// <summary>
    /// Mapper for data interface IReferenceData.
    /// </summary>
    public partial class ReferenceDataMapper<TDataEntity> : LunaMapper<TDataEntity, IReferenceData> where TDataEntity : IReferenceData
    {
        public ReferenceDataMapper()
        {
            GetAllRequest = DefaultGetAllRequest;
            GetRequest = DefaultGetRequest;
            InsertRequest = DefaultInsertRequest;
            UpdateRequest = DefaultUpdateRequest;
            DeleteRequest = DefaultDeleteRequest;
            GetAllByGoogleRequest = DefaultGetAllByGoogleRequest;
            GetAllByZohoCRMRequest = DefaultGetAllByZohoCRMRequest;
        }

        private const string DefaultGetAllRequest = "Meta.reference_select_all";
        private const string DefaultGetRequest = "Meta.reference_select";
        private const string DefaultInsertRequest = "Meta.reference_insert";
        private const string DefaultUpdateRequest = "Meta.reference_update";
        private const string DefaultDeleteRequest = "Meta.reference_delete";
        private const string DefaultGetAllByGoogleRequest = "Meta.reference_select_by_google";
        private const string DefaultGetAllByZohoCRMRequest = "Meta.reference_select_by_zohocrm";

        private const string LastUpdateColumnName = "LastUpdate";
        private const string GoogleColumnName = "Google";
        private const string GoogleLastUpdateColumnName = "GoogleLastUpdate";
        private const string ZohoCRMColumnName = "ZohoCRM";
        private const string ZohoCRMLastUpdateColumnName = "ZohoCRMLastUpdate";
        private const string FromColumnName = "From";
        private const string ValidatedColumnName = "Validated";

        public string GetAllByGoogleRequest { get; set; }

        public string GetAllByZohoCRMRequest { get; set; }

        protected override void LoadCoreData(IReferenceData entity, IDataRecord record)
        {
            entity.LastUpdate = record.GetDateAndTime(LastUpdateColumnName);
            entity.Google = record.GetString(GoogleColumnName);
            entity.GoogleLastUpdate = record.GetNullableDateAndTime(GoogleLastUpdateColumnName);
            entity.ZohoCRM = record.GetNullableInteger(ZohoCRMColumnName);
            entity.ZohoCRMLastUpdate = record.GetNullableDateAndTime(ZohoCRMLastUpdateColumnName);
            entity.From = record.GetEnum<ProviderType>(FromColumnName);
            entity.Validated = record.GetBoolean(ValidatedColumnName);
        }

        protected override void MapSaveParameters(IDatabaseCommand command, IReferenceData entity)
        {
            command.AddParameter(LastUpdateColumnName, entity.LastUpdate);
            command.AddParameter(GoogleColumnName, entity.Google);
            command.AddParameter(GoogleLastUpdateColumnName, entity.GoogleLastUpdate);
            command.AddParameter(ZohoCRMColumnName, entity.ZohoCRM);
            command.AddParameter(ZohoCRMLastUpdateColumnName, entity.ZohoCRMLastUpdate);
            command.AddParameter(FromColumnName, entity.From);
            command.AddParameter(ValidatedColumnName, entity.Validated);
            command.AddParameter(IDParameterName, entity.PK);
        }

        public IEnumerable<IReferenceData> GetAllByGoogle(IDbTransaction transaction, string google)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllByGoogleRequest,
                    c =>
                    {
                        c.AddParameter(GoogleColumnName, google);
                    });
        }

        public IEnumerable<IReferenceData> GetAllByZohoCRM(IDbTransaction transaction, int? zohoCRM)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllByZohoCRMRequest,
                    c =>
                    {
                        c.AddParameter(ZohoCRMColumnName, zohoCRM);
                    });
        }
    }
}